package leetcode.code1636;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
	public int[] frequencySort(int[] nums) {
		Map<Integer, Integer> cnts = new HashMap<>();
		for (int num : nums) {
			cnts.put(num, cnts.getOrDefault(num, 0) + 1);
		}
		Integer[] sort = new Integer[nums.length];
		for (int i = 0; i < sort.length; i++) {
			sort[i] = nums[i];
		}
		Arrays.sort(sort, (a, b) -> cnts.get(a) - cnts.get(b) == 0 ? b - a : cnts.get(a) - cnts.get(b));
		for (int i = 0; i < sort.length; i++) {
			nums[i] = sort[i];
		}
		return nums;
	}
}
